var context = null, ie6 = false;

function pie(context, title) {
  function className(className, context, tag){
    var rv = [], c, x, v, n, ii = 0, iz;
    n = (context || document).getElementsByTagName(tag || "*"), iz = n.length;
    x = " " + className + " ";
    for(; ii < iz; ++ii){
      v = n[ii];
      c = v.className;
      if (c) {
        if ((" " + c + " ").indexOf(x) !== -1) { rv.push(v); }
      }
    }
    return rv;
  }

  var ary = [
    className("add",    context, "li").length,
    className("fix",    context, "li").length,
    className("del",    context, "li").length,
    className("move",   context, "li").length,
    className("rename", context, "li").length,
    className("retake", context, "li").length,
    className("change", context, "li").length,
  ];
  var total = ary[0] + ary[1] + ary[2] + ary[3] + ary[4] + ary[5] + ary[6];
  if (!total) { return; }

  var url = [
    "http://chart.apis.google.com/chart",
    "?chs=360x200",              // width, height
    "&cht=p3&chtt=", title.replace(/\/.*$/, "").replace(/ /g, "+"), "|", total, "+Changes", // 3D pie
    "&chts=FFFFFF,20",           // title color, title size
    "&chl=Add|Fix|Delete|Move|Rename|Retake|Change",  // lable
    "&chco=7FFF00,87CEEB,A9A9A9,555555,FF66FF,FF0066,FFFF00",
    "&chf=bg,s,00000000",        // bg-color(00000000)
    "&chd=t:", ary[0], ",", ary[1], ",", ary[2], ",", ary[3], ",", ary[4], ",", ary[5], ",", ary[6]
  ].join("");

  var e = document.getElementById("pie");
  if (ie6) {
    e.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + url + '",sizingMethod=crop)';
    e.style.width  = "360px";
    e.style.height = "200px";
  } else {
    e.src = url;
  }
}

function boot() {
  function handleEvent(evt) {
    var tgt, tag;
    if (document.uniqueID) {
      evt.cancelBubble = true;
      evt.returnValue = false;
      tgt = evt.srcElement;
    } else {
      evt.stopPropagation();
      evt.preventDefault();
      tgt = evt.target;
    }
    tag = tgt.localName || tgt.tagName;
    if (tag === "DT") {
      if (context !== tgt) {
        context = tgt;
        pie(tgt.nextSibling, tgt.innerText || tgt.textContent);
      }
    }
  }

  if (document.uniqueID) {
    if (parseFloat(navigator.userAgent.match(/MSIE ([\d]\.[\d][\w]?)/)[1]) <= 6) {
      ie6 = true;
    }
  }

  if (document.uniqueID) {
    document.attachEvent("onmousemove", handleEvent);
    if (ie6) {
      document.createStyleSheet().cssText =
        ".posfixed { position: absolute; behavior: expression(this.style.top=(document.documentElement.scrollTop + 20)+'px') }"+
        "html, body { background-image: url(none); background-attachment: fixed }";
    }
  } else {
    document.addEventListener("mousemove", handleEvent, false);
  }

  pie(document.getElementById("version0.x-data"),
      document.getElementById("version0.x-title").innerText ||
      document.getElementById("version0.x-title").textContent);
}

window.onload = boot;

